import { Service } from "egg";

export default class GeweChatLogin extends Service {
  // 获取tokem
  async getTokenId() {
    const { ctx } = this;
    const client = await ctx.service.geweChat.client.getGeweChatSdk();
    const res = await client.exec("tools/getTokenId", {});
    const { ret, msg } = res;

    if (ret !== 200) {
      throw Error("msg");
    }

    return res;
  }

  // 获取登陆码
  async getLoginQrCode(wxGeweToken: string, params: { appId: string }) {
    const { ctx } = this;
    const client = await ctx.service.geweChat.client.getGeweChatSdk(wxGeweToken);

    const res = await client.exec("login/getLoginQrCode", params);

    return res;
  }
  // 登陆
  async checkLogin(wxGeweToken: string, params: { appId?: string; uuid?: string; captchCode?: string }) {
    const { ctx } = this;
    const client = await ctx.service.geweChat.client.getGeweChatSdk(wxGeweToken);
    const res = await client.exec("login/checkLogin", params);

    return res;
  }
  // 设置 url 回调地址
  async setCallback(wxGeweToken: string, params: { callbackUrl: string; token: string }) {
    const { ctx } = this;
    const client = await ctx.service.geweChat.client.getGeweChatSdk(wxGeweToken);
    const res = await client.exec("tools/setCallback", params);

    return res;
  }
}
